import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);


const store = new Vuex.Store({
    state:{
            // 列表    {msg:this.msg,isFinish:false,id:1}
            list:JSON.parse(localStorage.getItem("list")) || [],
            id:localStorage.getItem("id") || 1
    },
    mutations:{
            // 添加
            add(state,a){
                    Vue.set(a,"id",state.id);
                    state.list.push(a);
                    state.id++;
                    localStorage.setItem("list",JSON.stringify(state.list));
                    localStorage.setItem("id",state.id);
            },
            // 改变状态
            change(state,a){
                    state.list.forEach(elem => {
                        if(elem.id ==a.id){
                            elem.isFinish = a.check;
                            return;
                        }
                    });
                    localStorage.setItem("list",JSON.stringify(state.list));
            },
            // 删除
            del(state,id){
                    state.list.forEach((elem,index,arr)=>{
                        if(elem.id == id){
                            arr.splice(index,1);
                            return;
                        }
                        
                    });
                    localStorage.setItem("list",JSON.stringify(state.list));
            }

    },
    getters:{
        // 已完成的
            getFinish(state){
              var result =   state.list.filter((elem,index,arr)=>{
                       return elem.isFinish == true;
                });
                return result;
            },
        // 未完成
            getUnFinish(state){
                var result =   state.list.filter((elem,index,arr)=>{
                    return elem.isFinish == false;
             });
             return result;
            }
    }
});
export default store;